﻿using Dapper;
using System;
using BCEntity.Common.Business.WebsiteBusinessCategory;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.Common.Business.WebsiteBusinessCategory
{
    /// <summary>
    /// 官网业务分类
    /// </summary>
    public class WebsiteBusinessCategoryData : IWebsiteBusinessCategoryData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM websitebusinesscategory";
        private const string QUERY = @"SELECT WebsiteBusinessCategoryId,WebsiteBusinessTagcode,(SELECT Description FROM WebsiteBusinessTagcode B WHERE B.WebsiteBusinessTagcode=A.WebsiteBusinessTagcode LIMIT 1) AS WebsiteBusinessTagcodeDescription, 
                                            Title,Icon,Background,LinkUrl,Enabled,CreateAdminId,CreateAdminName,CreateTime,UpdateTime,EditAdminId,EditAdminName 
                                        FROM websitebusinesscategory A";
        public WebsiteBusinessCategoryData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查官网业务分类是否存在
        public async Task<bool> Exists(int websiteBusinessCategoryId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteBusinessCategoryId=?WebsiteBusinessCategoryId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteBusinessCategoryId", websiteBusinessCategoryId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加官网业务分类
        public async Task<WebsiteBusinessCategoryEntity> Add(WebsiteBusinessCategoryEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("websitebusinesscategory", new string[] { "WebsiteBusinessTagcode", "Title", "Icon","Background", "LinkUrl", "CreateAdminId", "CreateAdminName" });
            sql = string.Format("{0} {1} WHERE WebsiteBusinessCategoryId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteBusinessTagcode", entity.WebsiteBusinessTagcode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?Title", entity.Title?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?Icon", entity.Icon?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?Background", entity.Background?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?LinkUrl", entity.LinkUrl?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteBusinessCategoryEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改官网业务分类
        public async Task<WebsiteBusinessCategoryEntity> Update(WebsiteBusinessCategoryEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("websitebusinesscategory", new string[] { "WebsiteBusinessTagcode", "Title", "Icon", "Background", "LinkUrl", "EditAdminId", "EditAdminName" }, new string[] { "WebsiteBusinessCategoryId" });
            sql = string.Format("{0} {1} WHERE WebsiteBusinessCategoryId=?WebsiteBusinessCategoryId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteBusinessCategoryId", entity.WebsiteBusinessCategoryId, DbType.Int32, size: 11);
            parameters.Add("?WebsiteBusinessTagcode", entity.WebsiteBusinessTagcode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?Title", entity.Title?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?Icon", entity.Icon?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?Background", entity.Background?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?LinkUrl", entity.LinkUrl?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
            parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteBusinessCategoryEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除官网业务分类
        public async Task<bool> Delete(int websiteBusinessCategoryId)
        {
            string sql = @"DELETE FROM websitebusinesscategory WHERE WebsiteBusinessCategoryId=?WebsiteBusinessCategoryId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteBusinessCategoryId", websiteBusinessCategoryId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取官网业务分类
        public async Task<WebsiteBusinessCategoryEntity> GetEntity(int websiteBusinessCategoryId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteBusinessCategoryId=?WebsiteBusinessCategoryId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteBusinessCategoryId", websiteBusinessCategoryId, DbType.Int64);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteBusinessCategoryEntity>(sql, parameters);
        }
        #endregion

        #region 获取官网业务分类列表
        public async Task<IEnumerable<WebsiteBusinessCategoryEntity>> GetList(string tagcode)
        {
            if (string.IsNullOrWhiteSpace(tagcode))
            {
                string sql = string.Format(@"{0} WHERE Enabled=1", QUERY);
                return await this.databaseContext.Slave.QueryAsync<WebsiteBusinessCategoryEntity>(sql);
            }
            else
            {
                string sql = string.Format(@"{0} WHERE WebsiteBusinessTagcode=?WebsiteBusinessTagcode AND Enabled=1", QUERY);
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("?WebsiteBusinessTagcode", tagcode?.Trim(), DbType.AnsiString, size: 50);
                return await this.databaseContext.Slave.QueryAsync<WebsiteBusinessCategoryEntity>(sql, parameters);
            }
        }
        #endregion

        #region 获取官网业务分类分页
        public async Task<(IEnumerable<WebsiteBusinessCategoryEntity>, int)> Query(string tagcode,string title,DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            if (!string.IsNullOrWhiteSpace(tagcode))
            {
                wheres.Add("WebsiteBusinessTagcode LIKE CONCAT('%',CONCAT(?WebsiteBusinessTagcode,'%'))");
                parameters.Add("?WebsiteBusinessTagcode", tagcode?.Trim(), DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(title))
            {
                wheres.Add("Title LIKE CONCAT('%',CONCAT(?Title,'%'))");
                parameters.Add("?Title", title?.Trim(), DbType.AnsiString);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM websitebusinesscategory {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<WebsiteBusinessCategoryEntity, int>(sql, parameters);
        }
        #endregion

        #region 启用官网业务分类
        public async Task<bool> Enabled(int websiteBusinessCategoryId)
        {
            string sql = @"UPDATE websitebusinesscategory SET Enabled=1 WHERE WebsiteBusinessCategoryId=?WebsiteBusinessCategoryId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteBusinessCategoryId", websiteBusinessCategoryId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 禁用官网业务分类
        public async Task<bool> Disabled(int websiteBusinessCategoryId)
        {
            string sql = @"UPDATE websitebusinesscategory SET Enabled=0 WHERE WebsiteBusinessCategoryId=?WebsiteBusinessCategoryId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteBusinessCategoryId", websiteBusinessCategoryId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion
    }
}



